package com.ufo.store.management.ui.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.ufo.store.management.BarApplication;
import com.ufo.store.management.R;
import com.ufo.store.management.adapter.Isszambalv3MemberPointExpandAdapter;
import com.ufo.store.management.database.DatabaseManager;
import com.ufo.store.management.entity.Isszmbalv3Member;
import com.ufo.store.management.entity.Isszmbalv3MemberPointInfo;
import com.ufo.store.management.entity.Isszmbalv3MemberPointInfoGroup;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by taoli on 2017/12/29.
 */

public class Isszmbalv3MemberConsumptionActivity extends BaseActivity implements Handler.Callback {
    private final int REFRESH_DATA_FINISH = 11;
    private Handler mHandler = null;

    private LinearLayout mFailLl = null;
    private RelativeLayout mProcessTipLy = null;
    private ExpandableListView expandlistView = null;
    private Isszambalv3MemberPointExpandAdapter statusAdapter = null;
    private List<Isszmbalv3MemberPointInfoGroup> mIsszmbalv3MemberPointGroupInfoList = new ArrayList<Isszmbalv3MemberPointInfoGroup>();

    private Isszmbalv3Member mIsszmbalv3MemberInfo = null;

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case REFRESH_DATA_FINISH:
                if (statusAdapter != null) {
                    statusAdapter.setGroupData(mIsszmbalv3MemberPointGroupInfoList);
                    statusAdapter.notifyDataSetChanged();
                    expandlistView.setSelection(0);
                    int groupCount = expandlistView.getCount();
                    for (int i = 0; i < groupCount; i++) {
                        expandlistView.expandGroup(i);
                    }
                }
                break;
            default:
                break;
        }
        return false;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initView();
        Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
        toolbar.setTitle("消费记录");
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_back_selector);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initData() {
        mHandler = new Handler(this);
        Intent intent = getIntent();
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                mIsszmbalv3MemberInfo = (Isszmbalv3Member) bundle.getSerializable("Isszmbalv3MemberInfo");
            }
        }
    }

    private void initView() {
        setContentView(R.layout.activity_isszambalv3_member_consumption);

        mProcessTipLy = (RelativeLayout) findViewById(R.id.progress_tip_ly);
        mFailLl = (LinearLayout) findViewById(R.id.get_fail_tip_ly);
        expandlistView = (ExpandableListView) findViewById(R.id.expandlist);

        statusAdapter = new Isszambalv3MemberPointExpandAdapter(this, mIsszmbalv3MemberPointGroupInfoList);
        expandlistView.setAdapter(statusAdapter);
        expandlistView.setGroupIndicator(null);
        expandlistView.setSelection(0);
        int groupCount = expandlistView.getCount();
        // 首次加载展开第一项
        if (groupCount > 0) {
            expandlistView.expandGroup(0);
        }
        // return true不能点击收缩   return false 可以点击收缩
        expandlistView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return false;
            }
        });
        expandlistView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Isszmbalv3MemberPointInfo isszmbalv3MemberPointInfo = mIsszmbalv3MemberPointGroupInfoList.get(groupPosition).getChildList().get(childPosition);
                Intent toIntent = new Intent(Isszmbalv3MemberConsumptionActivity.this, Isszmbalv3SaleDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("Flowno", isszmbalv3MemberPointInfo.memberPointFlowno);
                toIntent.putExtras(bundle);
                startActivity(toIntent);
                return true;
            }
        });

        if (mIsszmbalv3MemberInfo != null && !TextUtils.isEmpty(mIsszmbalv3MemberInfo.memberCardId)) {
            new GetIsszmbalv3MemberPointInfoTask().execute(mIsszmbalv3MemberInfo.memberCardId);
        }
    }

    private class GetIsszmbalv3MemberPointInfoTask extends AsyncTask<String, Void, List<Isszmbalv3MemberPointInfoGroup>> {
        public GetIsszmbalv3MemberPointInfoTask() {
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mFailLl.setVisibility(View.GONE);
            mProcessTipLy.setVisibility(View.VISIBLE);
            expandlistView.setVisibility(View.GONE);
        }

        @Override
        protected List<Isszmbalv3MemberPointInfoGroup> doInBackground(String... params) {
            String memberIdStr = params[0];
            List<Isszmbalv3MemberPointInfo> data = null;
            if (BarApplication.getInstance().mDatabaseIsSqlServer) {
                data = DatabaseManager.getIsszmbalv3MemberPointInfoFromSqlServer(Isszmbalv3MemberConsumptionActivity.this, 0, memberIdStr);
            }
            List<Isszmbalv3MemberPointInfoGroup> group = null;
            if (data != null) {
                group = getListData(data);
            }
            return group;
        }

        @Override
        protected void onPostExecute(List<Isszmbalv3MemberPointInfoGroup> result) {
            super.onPostExecute(result);
            if (result != null) {
                mIsszmbalv3MemberPointGroupInfoList = result;
                Message _Msg = mHandler.obtainMessage(REFRESH_DATA_FINISH, 0);
                mHandler.sendMessage(_Msg);

                if( result.size() == 0 ){
                    mFailLl.setVisibility(View.VISIBLE);
                    mProcessTipLy.setVisibility(View.GONE);
                    expandlistView.setVisibility(View.GONE);
                }else{
                    mFailLl.setVisibility(View.GONE);
                    mProcessTipLy.setVisibility(View.GONE);
                    expandlistView.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    private List<Isszmbalv3MemberPointInfoGroup> getListData(List<Isszmbalv3MemberPointInfo> isszmbalv3MemberPointInfoList) {
        List<Isszmbalv3MemberPointInfoGroup> groupList = new ArrayList<Isszmbalv3MemberPointInfoGroup>();
        Map<String, String> map = new HashMap<String, String>();
        SimpleDateFormat format = new SimpleDateFormat("MM月dd日");
        for (Isszmbalv3MemberPointInfo isszmbalv3MemberPointInfo : isszmbalv3MemberPointInfoList) {
            if( TextUtils.isEmpty(isszmbalv3MemberPointInfo.memberPointFlowno))
                continue;

            String dateStr = format.format(isszmbalv3MemberPointInfo.memberPointOperdate);
            if (!map.containsKey(dateStr)) {
                Isszmbalv3MemberPointInfoGroup groupEntity = new Isszmbalv3MemberPointInfoGroup();
                groupEntity.setGroupName(dateStr);

                List<Isszmbalv3MemberPointInfo> childList = new ArrayList<Isszmbalv3MemberPointInfo>();
                childList.add(isszmbalv3MemberPointInfo);

                groupEntity.setChildList(childList);
                groupList.add(groupEntity);

                map.put(dateStr, dateStr);
            } else {
                for (Isszmbalv3MemberPointInfoGroup groupEntity : groupList) {
                    if (groupEntity.getGroupName().equals(dateStr)) {
                        List<Isszmbalv3MemberPointInfo> childList = groupEntity.getChildList();
                        childList.add(isszmbalv3MemberPointInfo);
                    }
                }
            }
        }
        return groupList;
    }
}

